package com.bj.zspace.bytebuddy.bbtest;

import com.bj.zspace.bytebuddy.normal_test.Service;
import net.bytebuddy.ByteBuddy;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.FixedValue;
import net.bytebuddy.matcher.ElementMatchers;

/**
 * 拦截实例方法
 **/
public class InstanceMethodClass {

    public static void main(String[] args) throws Exception {
        String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
        DynamicType.Unloaded<Service> unloaded = new ByteBuddy().subclass(Service.class).name("a.b.SubService")
            .method(ElementMatchers.named("toString")).intercept(FixedValue.value("bytebuddy: instance method"))
            .make();

        Class<? extends Service> loaded = unloaded.load(InstanceMethodClass.class.getClassLoader()).getLoaded();

        Service service = (Service)loaded.getDeclaredConstructors()[0].newInstance();

        System.out.println(service.toString());
    }

}
